package e.odbo.data.generator;

import java.util.Date;
import java.util.Random;

public abstract class Generator<T> implements I_Generator<T>{

    public  static I_Generator<Integer> INT=new Generator<Integer>() {
        @Override
        public java.lang.Integer generator() {
            return new Random().nextInt();
        }

        @Override
        public Integer generator(Integer base) {
            return new Random().nextInt(base);
        }

        @Override
        public String getName() {
            return "INT";
        }
    };

    public  static I_Generator<Double> DOUBLE=new Generator<Double>() {
        @Override
        public Double generator() {
            return new Random().nextDouble();
        }

        @Override
        public String getName() {
            return "DOUBLE";
        }
    };

    public  static  I_Generator<Long> LONG=new Generator<Long>() {
        @Override
        public Long generator() {
            return new Random().nextLong();
        }

        @Override
        public String getName() {
            return "LONG";
        }
    };

    public  static  I_Generator<Float> FLOAT =new Generator<Float>() {
        @Override
        public Float generator() {
            return new Random().nextFloat();
        }

        @Override
        public String getName() {
            return "FLOAT";
        }
    };

    public  static  I_Generator<Boolean> BOOLEAN=new Generator<Boolean>() {
        @Override
        public Boolean generator() {
            return new Random().nextBoolean();
        }

        @Override
        public String getName() {
            return "BOOLEAN";
        }
    };

    public static I_Generator<Date> NOW=new Generator<Date>() {
        @Override
        public Date generator() {
            return new Date();
        }

        @Override
        public String getName() {
            return "NOW";
        }
    };

    public static I_Generator<String> TELEPHONE=new TelephoneGenerator();

    public static I_Generator<String> EMAIL=new EmailGenerator();

    public static I_Generator<String> UUID=new Generator<String>() {
        @Override
        public String generator() {
            return java.util.UUID.randomUUID().toString();
        }

        @Override
        public String getName() {
            return "UUID";
        }
    };

    public static I_Generator<String> CARD_NUMBER(String prefix,int length){
        return new CardNumberGenerator(prefix,length);
    }
    public static I_Generator<String> CARD_NUMBER(String[] prefixs,int length){
        return new CardNumberGenerator(prefixs,length);
    }

    public static I_Generator<String> CREDITCARD_NUMBER(String prefix,int length){
        return new CreditCardNumberGenerator(prefix,length);
    }
    public static I_Generator<String> CREDITCARD_NUMBER(){
        return new CreditCardNumberGenerator();
    }

    public static I_Generator<String> STRING(int length){
        return new RoundStringGenerator(length);
    }

    public static I_Generator<String> STRING(int length,boolean symbol){
        return new RoundStringGenerator(length,symbol);
    }

    public static I_Generator<String> STRING(int length,String adv,boolean symbol){
        return new RoundStringGenerator(length,adv,symbol);
    }

    public static I_Generator<Date> DATE(){
        return new RandomDateGenerator();
    }

    public static I_Generator<Date> DATE(Date start){
        return new RandomDateGenerator(start);
    }

    public static I_Generator<Date> DATE(Date start,Date end){
        return new RandomDateGenerator(start,end);
    }

    public static I_Generator<String> PASSWORD(int length){
        return new PasswordGenerator(length);
    }

    public static I_Generator<String> ADV_PASSWORD(int length){
        return new PasswordGenerator(length,true);
    }

    public static I_Generator<String> CHINESS_IDNO(){
        return new ChineseIdNoGenerator((RandomDateGenerator) DATE());
    }

    public static I_Generator<String> CHINESS_IDNO(Date start){
        return new ChineseIdNoGenerator((RandomDateGenerator) DATE(start));
    }

    public static I_Generator<String> CHINESS_IDNO(Date start,Date end){
        return new ChineseIdNoGenerator((RandomDateGenerator) DATE(start,end));
    }

    public static <T> I_Generator<T>  DEFAULT(T value){
        return new DefaultValueGenerator<T>(value);
    }

    public static <T> I_Generator<T>  NULL(){
        return new DefaultValueGenerator<T>(null);
    }

    public static I_Generator InstanceForClass(Class classValue,Integer length) {
        if(classValue==Integer.class)
           return INT;
        if(classValue==Long.class)
            return LONG;
        if(classValue==Double.class)
            return DOUBLE;
        if(classValue==Date.class)
            return DATE();
        if(classValue==String.class)
            return STRING(length==null?20:length);
        if(classValue==Boolean.class)
            return BOOLEAN;
        if(classValue==Float.class)
            return FLOAT;
        return NULL();
    }


    @Override
    public T generator(T base) {
        return base;
    }


    public static class DefaultValueGenerator<T> extends Generator<T>{
        T value;

        public DefaultValueGenerator(T value) {
            this.value = value;
        }

        @Override
        public T generator() {
            return value;
        }

        @Override
        public String getName() {
            return "Default value:"+value;
        }
    }



    public abstract static class Increment<T extends Number> extends Generator<T>{
        protected T base;

        public Increment(T base) {
            this.base = base;
        }
    }

    public static  I_Generator<Integer> INCREMENT_INT(Integer baseNum){
        return new Increment<Integer>(baseNum){
            @Override
            public Integer generator() {
                return base++;
            }

            @Override
            public String getName() {
                return "INCREMENT_INT";
            }
        };
    }

    public static  I_Generator<Long> INCREMENT_LONG(Long baseNum){
        return new Increment<Long>(baseNum){
            @Override
            public Long generator() {
                return base++;
            }

            @Override
            public String getName() {
                return "INCREMENT_LONG";
            }
        };
    }

    public static  I_Generator<Short> INCREMENT_SHORT(Short baseNum){
        return new Increment<Short>(baseNum){
            @Override
            public Short generator() {
                return base++;
            }

            @Override
            public String getName() {
                return "INCREMENT_SHORT";
            }
        };
    }


}
